<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_AjaxAddBlockAction extends m_backoffice_extends_Action
{
	public function execute()
	{
		$rawValues = stripslashes($this->getRequest()->getParameter("value"));
		
		$documentId = $this->getRequestParameter('pageId');
		$documentLang = $this->getRequestParameter('pageLang');
		
		$tempDocService = m_backoffice_services_TempDocService::getInstance();

        $page = $tempDocService->getDocument($documentId, $documentLang);
	
		preg_match_all('`(.*?)="(.*?)"`', $rawValues, $params);

		foreach($params[0] as $blockParam)
		{
			list($paramName, $value) = explode("=", str_replace('"',"",trim($blockParam)));
			$result[][$paramName] = $value;
		}
		$result[]['rawValues'] = $rawValues;
		
		$content = $page->getContent();

		$this->fireInfo("{block ".$rawValues."}");
		
		$page->setContent("{block ".$rawValues."}".$content);
		
		$tempDocService->saveTemporaryDocument($page);
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo", "website_TabPageStructure"));
	}
}